package com.tianda.his.modules.erp.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianda.his.commons.utils.LoginUtils;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.erp.dto.MedicinesDto;
import com.tianda.his.modules.erp.dto.PruducerDto;
import com.tianda.his.modules.erp.entity.StockMedicines;
import com.tianda.his.modules.erp.service.StockMedicinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 药品信息管理
 */
@RestController
public class StockMedicinesController {



    @Autowired
    private StockMedicinesService medicinesService;

    /**
     * 分页查询
     * @param medicinesDto
     * @return
     */
    @GetMapping("/erp/medicines/listMedicinesForPage")
    public AjaxResult listMedicinesForPage(MedicinesDto medicinesDto) {
        Page<StockMedicines> pageResult = medicinesService.listMedicinesForPage(medicinesDto);
        return AjaxResult.success(pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 新增药品信息
     * @param stockMedicines
     * @return
     */
    @PostMapping("/erp/medicines/addMedicines")
    public AjaxResult addMedicines(StockMedicines stockMedicines) {
        stockMedicines.setCreateTime(new Date());
        stockMedicines.setCreateBy(LoginUtils.getLoginUser().getUserName());

        medicinesService.save(stockMedicines);
        return AjaxResult.success();

    }

    /**
     * 修改药品信息
     * @param stockMedicines
     * @return
     */
    @PutMapping("/erp/medicines/updateMedicines")
    public AjaxResult updateMedicines(StockMedicines stockMedicines) {
        stockMedicines.setUpdateTime(new Date());
        stockMedicines.setUpdateBy(LoginUtils.getLoginUser().getUserName());

        medicinesService.updateById(stockMedicines);
        return AjaxResult.success();
    }

    /**
     * 根据id查询
     * @param medicinesId
     * @return
     */
    @GetMapping("/erp/medicines/getMedicinesById/{medicinesId}")
    public AjaxResult getMedicinesById(@PathVariable("medicinesId") Long medicinesId) {
        return AjaxResult.success(medicinesService.getById(medicinesId));
    }

    /**
     * 删除药品信息
     * @param medicinesId
     * @return
     */
    @DeleteMapping("/erp/medicines/deleteMedicinesByIds/{medicinesId}")
    public AjaxResult deleteMedicinesById(@PathVariable("medicinesId") Long medicinesId) {
        medicinesService.removeById(medicinesId);
        return AjaxResult.success();
    }

    /**
     * 修改库存
     * @param medicinesId 药品id
     * @param stock 库存
     * @return
     */
    @PostMapping("/erp/medicines/updateMedicinesStorage/{medicinesId}/{stock}")
    public AjaxResult updateMedicinesStorage(@PathVariable Long medicinesId, @PathVariable Integer stock) {
        StockMedicines stockMedicines = new StockMedicines();
        stockMedicines.setMedicinesId(medicinesId);
        stockMedicines.setMedicinesStockNum(stock);
        medicinesService.updateById(stockMedicines);
        return AjaxResult.success();
    }
}
